/**
 * Copyright 2015 ZhangQu Li
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package pers.julio.notepad.PageSwitcher;

import android.content.Context;
import android.content.Intent;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import pers.julio.notepad.PageSwitcher.Base.BaseActivity;

/**
 * ClassName:  PageConfig
 * Description: <全局配置类>
 * Author: julio_chan  2020/10/20 12:27
 */
public class PageConfig {
    public final static String ACTION_EXIT_APP = "pers.julio.notepad.PageSwitcher.exit";
    private static Context mContext;
    private boolean hasInited=false;

    private static boolean isOpenAtlas=false;
    public static boolean isOpenAtlas() { return isOpenAtlas; }
    public static void setIsOpenAtlas(boolean isOpenAtlasFlag) { isOpenAtlas = isOpenAtlasFlag; }

    private static ClassLoader mBundleClassLoader =null;
    public static ClassLoader getBundleClassLoader() {
        return mBundleClassLoader;
    }
    public static void setBundleClassLoader(ClassLoader classLoader) { mBundleClassLoader = classLoader; }

    //本地广播退出
    private static LocalBroadcastManager mLocalBroadcatManager;
    public static LocalBroadcastManager getLocalBroadcastManager() {
        if (mLocalBroadcatManager == null) {
            mLocalBroadcatManager = LocalBroadcastManager.getInstance(mContext);
        }
        return mLocalBroadcatManager;
    }

    // 加载页面配置文件 在assets/page.json
    public static void init(Context context,String pageJson){
        mContext=context.getApplicationContext();
        PageManager.getInstance().init(mContext,pageJson);
    }
    public static void init(Context context){
        mContext=context.getApplicationContext();
        PageManager.getInstance().init(mContext);
    }


    public static void unInit(){
        Intent intent = new Intent();
        intent.setAction(PageConfig.ACTION_EXIT_APP);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        getLocalBroadcastManager().sendBroadcast(intent);
        BaseActivity.unInit();
        mLocalBroadcatManager=null;
    }

    // 发送本地广播退出程序
    public void exitApp() {
        Intent intent = new Intent();
        intent.setAction(PageConfig.ACTION_EXIT_APP);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        getLocalBroadcastManager().sendBroadcast(intent);
        BaseActivity.unInit();
    }
}
